<?php

namespace app\admin\controller\pay;

use app\common\controller\Backend;

use think\Controller;
use think\Request;

/**
 * 支出明细
 *
 * @icon fa fa-circle-o
 */
class Index extends Backend
{

    /**
     * Pay模型对象
     */
    protected $model = null;
    protected $projectModel = null;

    protected $searchfields;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Pay');
        $this->projectModel = model('PayProject');
        $this->view->assign('project', $this->projectModel->field('id,name')->select());
        $pid = input('pid','0', 'intval');    
       
        $this->view->assign("pid", $pid);
    }

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name')) {
                return $this->selectpage();
            }
            return $this->handleSearch($this->searchfields);
        }
        return $this->view->fetch();
    }

    public function add()
    {
        return parent::create('form');
    }

    public function edit($ids = null)
    {
        return parent::modify($ids, 'form');
    }

    public function del($ids = null){
        $where = array(
            'id' => array('in',$ids)
        );
        parent::delete($where);
    }

    protected function handleSearch($searchfields)
    { 
        $pid = input('pid','0', 'intval');    
        $append = array(
           
        );
        $append = array_merge($append, $this->buildCommonSearch());
        list($where, $sort, $order, $offset, $limit,$orderParams) = $this->buildparams($searchfields, null, $append);
        $total = $this->model->where($where)->count();
        $list = $this->model->with('project')->where($where)->order($orderParams)->limit($offset, $limit)->select();
        $result = array("total" => $total, "rows" => $list);
        return json($result);
    }

    public function ListPay()
    {
       $list=$this->model->field(['id as value,summary as label'])->select();
      
       return  json_encode($list);
    }

  
    private function buildCommonSearch()
    {
        $where = array();
        $pid = input('pid','0', 'intval');    
        if($pid>0)
        {
            $where[] = array('project_id', '=', $pid);
        }
        $searchs = $this->request->request('query/a');
        if ($searchs['pid']) {
            $where[] = array('project_id', '=', $searchs['pid']);
        }
        return $where;
    }
}
